package com.example.demo.controller;

import com.example.demo.model.Forum;
import com.example.demo.service.ForumService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/forums")
public class ForumController {

    private final ForumService forumService;

    // 构造函数注入 ForumService，实现依赖注入
    public ForumController(ForumService forumService) {
        this.forumService = forumService;
    }

    @GetMapping("/all")
    public ResponseEntity<List<Map<String, Object>>> getAllForums() {
        List<Forum> forums = forumService.getAllForums();
        if (forums.isEmpty()) {
            return ResponseEntity.noContent().build();
        }
        List<Map<String, Object>> result = new ArrayList<>();
        for (Forum forum : forums) {
            Map<String, Object> map = new HashMap<>();
            map.put("parent_id", forum.getParentId());
            map.put("userId", forum.getUserId());
            map.put("username", forum.getUsername());
            map.put("title", forum.getTitle());
            map.put("content", forum.getContent());
            map.put("createTime", forum.getCreateTime() != null ? forum.getCreateTime().toString() : "");
            map.put("replyTime", forum.getReplyTime() != null ? forum.getReplyTime().toString() : "");
            result.add(map);
        }
        return ResponseEntity.ok(result);
    }

    // 根据ID获取论坛帖子
    @GetMapping("/{id}")
    public ResponseEntity<Forum> getForumById(@PathVariable Long id) {
        Forum forum = forumService.getForumById(id);
        if (forum == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(forum);
    }

    // 根据用户名获取论坛帖子（示例，实际可返回列表）
    @GetMapping("/user/{userId}")
    public ResponseEntity<List<Forum>> getForumByUsername(@PathVariable String username) {
        List<Forum> forums = forumService.getForumByUsername(username);
        if (forums.isEmpty()) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(forums);
    }

    // 新增论坛帖子
    @PostMapping("/create")
    public ResponseEntity<String> createForum(@RequestBody Forum forum) {
        forumService.createForum(forum);
        return ResponseEntity.ok("Forum created successfully with ID: " + forum.getId());
    }

    // 更新论坛帖子
    @PutMapping("/{id}")
    public ResponseEntity<String> updateForum(@PathVariable Long id, @RequestBody Forum forum) {
        Forum existing = forumService.getForumById(id);
        if (existing == null) {
            return ResponseEntity.notFound().build();
        }
//        forum.setId(id); // 确保ID一致
        forumService.updateForum(forum);
        return ResponseEntity.ok("Forum updated successfully.");
    }

    // 删除论坛帖子
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteForum(@PathVariable Long id) {
        Forum existing = forumService.getForumById(id);
        if (existing == null) {
            return ResponseEntity.notFound().build();
        }
        forumService.deleteForum(id);
        return ResponseEntity.ok("Forum deleted successfully.");
    }
}
